import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/tools/locale_tool.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'app_remind_logic.dart';

class AppRemindPage extends StatelessWidget {
  AppRemindPage({super.key});

  final logic = Get.put(AppRemindLogic());
  final state = Get
      .find<AppRemindLogic>()
      .state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(LocaleKeys.settings_app_remind.ts,
          style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back()),
      ),
      body: GetBuilder<AppRemindLogic>(builder: (logic) {
        return SafeArea(
          child: _buildList(state.installedApps),
        );
      }),
    );
  }

  Widget _buildList(List<dynamic> appList) {
    state.appRemindValue[UTEPedConst.appRemindOtherId] =
        logic.read(UTEPedConst.appRemindOtherId, defaultValue: false);

    List<Widget> items = [];
    items.add(Text(LocaleKeys.app_remind_des.ts,
      style: TextStyle(fontSize: 42.sp, color: Color(0xFF898C8F)),));
    int count = (appList.length / 6).ceil();
    for (int i = 0; i < count; i++) {
      List<Widget> subItems = [];
      for (int j = i * 6; j < i * 6 + 6 && j < appList.length; j++) {
        Map<String, dynamic> appInfo = appList[j];
        var appName = LocaleTool.isCN() ? appInfo[UTEPedConst.keyName]: appInfo[UTEPedConst.keyNameEN];
        var packageName = appInfo[UTEPedConst.keyPackageName];
        Widget subWidget = _buildItem(Image
            .asset(appInfo["icon"])
            .image, appName,
            packageName,
            onChange: (value) {
              state.appRemindValue[packageName] = value;
              logic.switchOnChange(packageName, value);
            });
        subItems.add(subWidget);
        if (j < i * 6 + 6 - 1 && j < appList.length - 1) {
          subItems.add(Divider(height: 1.w,));
        }
      }

      Widget widget = Container(
          margin: EdgeInsets.only(top: 59.h),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              color: Colors.white
          ),
          child: Column(
            children: subItems,
          )
      );
      items.add(widget);
    }

    Widget tips = Container(
      margin: EdgeInsets.only(top: 99.h),
      child: Text(LocaleKeys.app_remind_other_des.ts,
        style: TextStyle(fontSize: 42.sp, color: ColorConst.btnFontColor),),
    );

    Widget other = Container(
      margin: EdgeInsets.only(top: 59.h),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: Colors.white
      ),
      child: _buildItem(
          const AssetImage("assets/images/device/remind_other.png"),
          LocaleKeys.app_remind_other.ts,
          UTEPedConst.appRemindOtherId,
          onChange: (value) {
            state.appRemindValue[UTEPedConst.appRemindOtherId] = value;
            logic.switchOnChange(UTEPedConst.appRemindOtherId, value);
          }),
    );

    items.add(tips);
    items.add(other);
    return ListView(
      padding: EdgeInsets.fromLTRB(42.w, 106.h, 42.w, 322.h),
      children: items,
    );
  }

  Widget _buildItem(ImageProvider image, String title, String id, {onChange}) {
    Widget widget = Container(
        padding: EdgeInsets.only(left: 60.w, right: 60.w),
        height: 200.h,
        child: Row(
            children: [
              CircleAvatar(
                radius: 50.w,
                backgroundImage: image,
              ),
              SizedBox(width: 40.w,),
              Text(title, style: TextStyle(
                  fontSize: 42.sp, color: ColorConst.btnFontColor),),
              Expanded(child: Container()),
              GetBuilder<AppRemindLogic>(
                  id: id,
                  builder: (_) {
                    return Transform.scale(
                      scale: 0.7,
                      child: CupertinoSwitch(value: state.appRemindValue[id] == true,
                          activeColor: ColorConst.activeColor,
                          onChanged: onChange),
                    );
                  })

            ]));
    return widget;
  }

}
